import com.alibaba.fastjson.JSONObject;
import com.nh.common.pojo.Log;
import com.nh.log.util.HttpClientUtil;
import org.junit.Test;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: LaoCao
 * @Date: 2023/3/15 23:15
 * @Version: 1.0
 **/
//@SpringBootTest(classes = PrintAndAnalyseMain.class)
//@RunWith(SpringRunner.class)
public class testAA {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @org.junit.jupiter.api.Test
    public void redisTest(){
        Long num = redisTemplate.opsForValue().increment("num");
        System.out.println(num);
    }

    @Test
    public void test() throws IOException {
        File fileA = new File("D:\\log\\logA\\A.log");
        File fileB = new File("D:\\log\\logB\\B.log");

        Map<Long, Log> logAMap = getLogMap(fileA);
        Map<Long, Log> logBMap = getLogMap(fileB);
        System.out.println(logAMap);
        System.out.println(logBMap);

    }

    private Map<Long,Log> getLogMap(File file) throws IOException {
        BufferedReader fileReader = new BufferedReader(new FileReader(file));
        String line;
        char[] c = new char[1024];
        StringBuilder stringBuilder = new StringBuilder();
        while(null != (line = fileReader.readLine())) {
            stringBuilder.append(line).append("\n");
        }
        String s = stringBuilder.toString();
        String[] split = s.split("\n");

        Map<Long,Log> logMap = new HashMap<>();
        for (String s1 : split) {
            if (s1.contains("\"id\"")) {
//                System.out.println(s1);
                System.out.println();
                String[] s2 = s1.split(" ");
                Log log = JSONObject.parseObject(s2[s2.length - 1], Log.class);
                log.setLogDateTime(s2[0] + " " + s2[1]);
                System.out.println(log);
                logMap.put(log.getLogId(),log);
            }
        }
        return logMap;
    }

    @Test
    public void logTest() throws IOException {
        File fileA = new File("D:\\log\\logA");
        File[] files = fileA.listFiles();
        assert files != null;
        for (File file : files) {
            System.out.println(file.getName());
        }
//        FileReader fileReader = new FileReader(fileA);
//        int index = -1;
//        char[] c = new char[1024];
//        StringBuilder stringBuilder = new StringBuilder();
//        while(index != fileReader.read(c)) {
//            stringBuilder.append(c);
//        }
//        String s = stringBuilder.toString();

//        System.out.println(s);
    }

    @Test
    public void timeTest() throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String format = simpleDateFormat.format(new Date());
        System.out.println(format);


    }

    @Test
    public void HttpTest(){
        String s = HttpClientUtil.doGet("http://localhost/api/log/print");
        System.out.println(s);
    }


    @Test
    public void listTest(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 100000; i++) {
            list.add(i);
        }
//
//        List<Integer> list1 = list.subList(2, 6);
//        System.out.println("list1 = " + list1);
//        System.out.println("list = " + list);
        int maxNum = list.size();
        int num = 0;
        while (maxNum > 0){
            if (maxNum >= 10000){
                List<Integer> list1 = list.subList(num, num += 10000);
                System.out.println("list1 = " + list1.get(0));
                System.out.println("num = " + num);
                System.out.println("maxNum = " + maxNum);
                maxNum-= 10000;
            }
        }
    }
}
